package com.sinosoft.msf.base.supplier.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.sinosoft.msf.base.supplier.dao.base.SCompanyUserRoleMapper;
import com.sinosoft.msf.base.supplier.pojo.entity.SCompanyUserRole;
import com.sinosoft.msf.base.supplier.pojo.OptVo;
import com.sinosoft.msf.base.supplier.props.UserRoleEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CompanyUserRoleService {
    @Autowired
    SCompanyUserRoleMapper roleMapper;

    public Set<String> queryRole(OptVo optVo) {
        SCompanyUserRole userRole = new SCompanyUserRole();
        userRole.setUserId(optVo.getUserId());
        List<SCompanyUserRole> select = roleMapper.select(userRole);
        if (CollUtil.isEmpty(select)) {
            return new HashSet<>();
        }
        return select.stream().map(SCompanyUserRole::getRoleCode).collect(Collectors.toSet());
    }
    public List<String> queryIdByRole(UserRoleEnum roleEnum) {
        SCompanyUserRole userRole = new SCompanyUserRole();
        userRole.setRoleCode(roleEnum.name());
        List<SCompanyUserRole> select = roleMapper.select(userRole);
        if (CollUtil.isEmpty(select)) {
            return new ArrayList<>();
        }
        return select.stream().map(SCompanyUserRole::getUserId).distinct().collect(Collectors.toList());
    }

    public String addRole(SCompanyUserRole userRole) {
        userRole.setId(IdUtil.simpleUUID());
        userRole.setDeleted("n");
        userRole.setCreatedDate(DateUtil.date());
        roleMapper.insert(userRole);
        return "添加成功";
    }
    public String delRole(SCompanyUserRole userRole) {
        SCompanyUserRole sCompanyUserRole = roleMapper.selectByPrimaryKey(userRole);
        if (sCompanyUserRole != null) {
            sCompanyUserRole.setDeleted("y");
            roleMapper.updateByPrimaryKeySelective(sCompanyUserRole);
            return "删除成功";
        }

        return "不存在该角色";
    }
}
